<?php
/**
 * @package		Joomla.Site
 * @subpackage	com_forum
 * @copyright	Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
 * @license		GNU General Public License version 2 or later; see LICENSE.txt
 */

 /* Weblinks Component Route Helper
 *
 * @package		Joomla.Site
 * @subpackage	com_forum
 * @since 1.6
 */

defined('_JEXEC') or die;

jimport('joomla.application.categories');

/**
 * Build the route for the com_forum component
 *
 * @param	array	An array of URL arguments
 *
 * @return	array	The URL arguments to use to assemble the subsequent URL.
 */
function ForumBuildRoute(&$query)
{
	$segments	= array();

	// get a menu item based on Itemid or currently active
	$app		= JFactory::getApplication();
	$menu		= $app->getMenu();
	$params		= JComponentHelper::getParams('com_forum');

	// we need a menu item.  Either the one specified in the query, or the current active one if none specified
	if (empty($query['Itemid'])) {
		$menuItem = $menu->getActive();
		$menuItemGiven = false;
	}
	else {
		$menuItem = $menu->getItem($query['Itemid']);
		$menuItemGiven = true;
	}

	if (isset($query['view'])) {
		$view = $query['view'];
	}
	else {
		// we need to have a view in the query or it is an invalid URL
		return $segments;
	}

	// are we dealing with an article or category that is attached to a menu item?
	if (($menuItem instanceof stdClass) && $menuItem->query['view'] == $query['view'] && isset($query['id']) && $menuItem->query['id'] == intval($query['id'])) {
		unset($query['view']);

		if (isset($query['catid'])) {
			unset($query['catid']);
		}
		
		if (isset($query['layout'])) {
			unset($query['layout']);
		}

		unset($query['id']);

		return $segments;
	}
	
	if($view == 'category')
	{
		$catid = $query['id'];
		$categories = JCategories::getInstance('Forum');
		$category = $categories->get($catid);

		//surement il y a catid, tid, id

		if (!$category) {
			// we couldn't find the category we were given.  Bail.
			return $segments;
		}

		$path = $category->getPath();
		$segments[] = str_replace(':', '-', $path);
		
		return $segments;
	}
	elseif($view == 'topic')
	{
		
		
		if(!isset($query['catid']) || !isset($query['id']))
		{
			return $segments;
		}
		
		$catid = $query['catid'];
		$categories = JCategories::getInstance('Forum');
		$category = $categories->get($catid);

		//surement il y a catid, tid, id

		if (!$category) {
			// we couldn't find the category we were given.  Bail.
			return $segments;
		}

		$path = $category->getPath();
		
		$segments[] = str_replace(':', '-', $path[0]);
		
		$segments[] = $query['id'];

		unset($query['view']);
		unset($query['catid']);
		unset($query['id']);
		return $segments;
	
	
	
	}
	elseif($view == 'post')
	{

		if(!isset($query['catid']) || !isset($query['tid']))
		{
			return $segments;
		}
		
		$catid = $query['catid'];
		$categories = JCategories::getInstance('Forum');
		$category = $categories->get($catid);

		//surement il y a catid, tid, id

		if (!$category) {
			// we couldn't find the category we were given.  Bail.
			return $segments;
		}

		$path = $category->getPath();
		
		$segments[] = str_replace(':', '-', $path[0]);
		
		$segments[] = $query['tid'];
		if(isset($query['id']))
		{
			$segments[] = $query['id'];
			unset($query['id']);
		}
		
		unset($query['view']);
		unset($query['catid']);
		unset($query['tid']);
		
		if(isset($query['layout']))
		{
			$segments[] = $query['layout'];
			unset($query['layout']);
		}
		
		return $segments;
	
	}
	
	
}
/**
 * Parse the segments of a URL.
 *
 * @param	array	The segments of the URL to parse.
 *
 * @return	array	The URL attributes to be used by the application.
 */
function ForumParseRoute($segments)
{
	$vars = array();

	//Get the active menu item.
	$app	= JFactory::getApplication();
	$menu	= $app->getMenu();
	$item	= $menu->getActive();
	$params = JComponentHelper::getParams('com_forum');
	$advanced = $params->get('sef_advanced_link', 0);

	// Count route segments
	$count = count($segments);

	// From the categories view, we can only jump to a category.
	//var_dump($item->query);
	$id = (isset($item->query['id']) && $item->query['id'] > 1) ? $item->query['id'] : 'root';
	if(isset($segments[0])) //category
	{
		$category = JCategories::getInstance('Forum')->get($id);

		$categories = $category->getChildren();
		foreach($categories as $category)
		{
			if (($category->slug == $segments[0]) || ($advanced && $category->alias == str_replace(':', '-', $segments[0]))) {
				$vars['catid'] = $category->id;
				$vars['id'] = $category->id;
				$vars['view'] = 'category';
				$categories = $category->getChildren();
				$catfound = 1;

				break;
			}
		}
	}

	if(isset($segments[1]))	//topic
	{
		$vars['view'] = 'topic';
		$vars['id'] = $segments['1'];
		$vars['tid'] = $segments['1'];
	}

	
	if(isset($segments[2]))
	{
		$vars['view'] = 'post';
		$vars['layout'] = $segments[2];
		unset($vars['id']);
	}
	
	if(isset($segments[3]))
	{
		$vars['id'] = $segments[3];
	}

	return $vars;
}
